<template>
  <div class="view-fill">
    <PageDialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-list="dialogInfo.btList"
    >
      <!-- 条件栏 -->
      <NewPageFilter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
        :filter-btn="filterInfo.btn"
      >
      </NewPageFilter>
      <DialogTable
        v-model:data="tableInfo.data"
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        :api="tableInfo.api"
        :pager="true"
        :check-box="true"
        :highlight-row="true"
        :query="filterInfo.query"
        :tab-index="true"
        :field-list="tableInfo.fieldList"
        :isRowClickSelect="true"
        row-key="id"
        @handleEvent="handleEvent"
      />
    </PageDialog>
  </div>
</template>

<script setup name="changeAnalysisPlans">
import * as echarts from 'echarts'
import moment from 'moment'
import { getTripartiteSystemProjectPage } from '@/api/tripartite.js'

const { proxy } = getCurrentInstance()
const props = defineProps(['visible', 'id', 'query'])
// 过滤相关配置
const filterInfo = ref({
  query: proxy.$fn.deepClone(props.query),
  copyQuery: {},
  list: [
    {
      componentType: 'input',
      event: search,
      hidden: true,
      label: '项目编码',
      show: true,
      value: 'tsProjectCodeLike',
    },
    {
      componentType: 'input',
      event: search,
      hidden: true,
      label: '项目名称',
      show: true,
      value: 'tsProjectNameLike',
    },
    {
      componentType: 'select-model',
      label: '数据来源',
      value: 'dataSourceMult',
      code: 'data_source',
      multiple: true,
      event: search,
    },
    {
      label: '是否对照',
      value: 'isCompare',
      componentType: 'select-model',
      code: 'yes_or_no',
      event: search,
      show: true,
      hidden: true,
    },
  ],
  btn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: '选择项目',
  btLoading: false,
  width: '800px',
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      event: save,
      show: true,
    },
  ],
})
// 分析项目表格
const tableInfo = ref({
  api: null,
  refresh: 1,
  initCurpage: 1,
  data: [],
  selections: [],
  fieldList: [
    { label: '项目编码', value: 'tsProjectCode' },
    { label: '项目名称', value: 'tsProjectName' },
    { label: '单价', value: 'tsPrice' },
    { label: '保留1', value: 'reserved1' },
    { label: '保留2', value: 'reserved2' },
    { label: '保留3', value: 'reserved3' },
    { label: '保留4', value: 'reserved4' },
    { label: '保留5', value: 'reserved5' },
    { label: '数据来源', value: 'dataSourceValue' },
    { label: '是否对照', value: 'isCompareValue' },
    { label: '最新对照时间', value: 'compareTime' },
    { label: '创建人', value: 'createUserName' },
    { label: '创建时间', value: 'createTime' },
  ],
})

const shows = computed({
  get() {
    return props.visible
  },
  set(val) {
    proxy.$emit('update:visible', val) // 更新父组件visible
  },
})

onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    getList()
  })
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
})
function clearSearch() {
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQuery)
  search()
}
// 获取列表
function getList() {
  tableInfo.value.api = getTripartiteSystemProjectPage
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function save() {
  handleClose()
  proxy.$emit('callback', tableInfo.value.selections)
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = data
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  proxy.$emit('reset')
}
</script>
<style scoped lang="scss">
:deep(.table-scroll-hidden) > .el-table {
  overflow-y: hidden;
}
:deep(.el-dialog__body) {
  height: 355px;
  display: flex;
  flex-direction: column;
}
</style>
